package com.maven.front.service.emailgli;




import com.maven.front.bean.EduEmailsendHistory;

import com.maven.front.bean.Result;
import com.maven.front.bean.SysUser;
import com.maven.front.dto.EduEmailsendHistoryDto;
import com.maven.front.mapper.EduEmailsendHistoryMapper;

import com.maven.front.mapper.SysUserMapper;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sun.rmi.runtime.Log;


import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;




import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.*;



@Slf4j
@Service
public class EduEmailsendHistoryService {

    @Resource
    private EduEmailsendHistoryMapper eduEmailsendHistoryMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    JavaMailSender javaMailSender;

    //id查询
    public EduEmailsendHistory selectByPrimaryKey(Integer id){
        //传入id获取主表,副表一条数据
        return   eduEmailsendHistoryMapper.selectByPrimaryKey(id);
    }

    //修改
    public int update(EduEmailsendHistory eduEmailsendHistory){
        return eduEmailsendHistoryMapper.update(eduEmailsendHistory);
    }

    //查询定时为发送的
    public  List<EduEmailsendHistory> select1(EduEmailsendHistory eduEmailsendHistory){

        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        List<EduEmailsendHistory> list=this.eduEmailsendHistoryMapper.select1(eduEmailsendHistory);
        System.out.println(list.size()+"---------------");
        System.out.println(list.get(0).getId());
        if(!CollectionUtils.isEmpty(list)){
            //发送者
            simpleMailMessage.setFrom("2430562424@qq.com");
            for (int i=0;i<=list.size();i++){
                //接收者,setTo群发
                simpleMailMessage.setTo(list.get(i).getEmail());
                //设置邮件主题
                simpleMailMessage.setSubject(list.get(i).getTitle());
                //设置邮件主题内容
                simpleMailMessage.setText(list.get(i).getContent());
                //设置时间
                simpleMailMessage.setSentDate(eduEmailsendHistory.getSendTime());
                //发送
                javaMailSender.send(simpleMailMessage);
                eduEmailsendHistory.setSendTime(new Date());
                eduEmailsendHistory.setId(list.get(i).getId());
                eduEmailsendHistoryMapper.update(eduEmailsendHistory);
            }
        }
        return eduEmailsendHistoryMapper.select1(eduEmailsendHistory);
    }

    //新增
    public int insert(EduEmailsendHistory eduEmailsendHistory){
        System.out.println(eduEmailsendHistory.getUserId()+":"+eduEmailsendHistory.getContent()+":"+eduEmailsendHistory.getTitle()+":"+eduEmailsendHistory.getEmail());
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        String [] user=eduEmailsendHistory.getEmail().split(",");
        System.out.println(user.length);
        if(eduEmailsendHistory.getType()==1){
            //发送者
            simpleMailMessage.setFrom("2430562424@qq.com");
            //接收者,setTo群发
            simpleMailMessage.setTo(user);
            //设置邮件主题
            simpleMailMessage.setSubject(eduEmailsendHistory.getTitle());
            //设置邮件主题内容
            simpleMailMessage.setText(eduEmailsendHistory.getContent());
            //设置时间
            simpleMailMessage.setSentDate(new Date());
            javaMailSender.send(simpleMailMessage);
            eduEmailsendHistory.setSendTime(new Date());
            eduEmailsendHistory.setCreateTime(new Date());
            eduEmailsendHistory.setStatus(1);
            for (int a=0;a<user.length;a++) {
                //发送人存入数据库
                eduEmailsendHistory.setEmail(user[a]);
                eduEmailsendHistoryMapper.insert(eduEmailsendHistory);
            }
        }else{
            eduEmailsendHistory.setStatus(2);
            eduEmailsendHistory.setType(2);
            eduEmailsendHistory.setSendTime(eduEmailsendHistory.getSendTime());
            eduEmailsendHistory.setCreateTime(new Date());
            //发送者
            simpleMailMessage.setFrom("2430562424@qq.com");
            //接收者,setTo群发
            simpleMailMessage.setTo(user);
            //设置邮件主题
            simpleMailMessage.setSubject(eduEmailsendHistory.getTitle());
            //设置邮件主题内容
            simpleMailMessage.setText(eduEmailsendHistory.getContent());
            //设置时间
            simpleMailMessage.setSentDate(eduEmailsendHistory.getSendTime());
            for (int a=0;a<user.length;a++) {
                //发送人存入数据库
                eduEmailsendHistory.setEmail(user[a]);
                eduEmailsendHistoryMapper.insert(eduEmailsendHistory);
            }

        }
        return 0;
    }

    public List<EduEmailsendHistoryDto> listAll(EduEmailsendHistory eduEmailsendHistory) {
        List<EduEmailsendHistoryDto> listEduEmailsendHistoryDto=new ArrayList<>();
        List<EduEmailsendHistory> listEduEmailsendHistory=eduEmailsendHistoryMapper.select(eduEmailsendHistory);
        for (int i=0;i<listEduEmailsendHistory.size();i++){
            String name=sysUserMapper.selectByPrimaryKey(listEduEmailsendHistory.get(i).getUserId()).getLoginName();
            EduEmailsendHistoryDto eduEmailsendHistoryDto=new EduEmailsendHistoryDto();
            //  将父类数据复制给子类数据
            BeanUtils.copyProperties(listEduEmailsendHistory.get(i),eduEmailsendHistoryDto);
            //将获取的名称赋值给子类的新属性(name)
            eduEmailsendHistoryDto.setName(name);
            listEduEmailsendHistoryDto.add(eduEmailsendHistoryDto);
        }
        if(listEduEmailsendHistoryDto!=null){
            return listEduEmailsendHistoryDto;
        }else{
            return null;
        }

    }



    public List<EduEmailsendHistory> select(EduEmailsendHistory eduEmailsendHistory){
        return this.eduEmailsendHistoryMapper.select(eduEmailsendHistory);
    }
}
